package vip.liux.backend.adapter.controllers;

import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import vip.liux.backend.application.service.shop.AddressDto;
import vip.liux.backend.application.service.shop.AddressService;
import vip.liux.backend.application.service.shop.ModifyingAddressDto;

import java.util.List;

@RestController
@RequestMapping("/api/address")
public class AddressController {
    private final AddressService addressService;

    public AddressController(AddressService addressService) {
        this.addressService = addressService;
    }

    @GetMapping("/list")
    public List<AddressDto> list() {
        return addressService.list();
    }

    @PostMapping
    public AddressDto add(@Valid @RequestBody ModifyingAddressDto addressDto) {
        return addressService.add(addressDto);
    }

    @PutMapping("/{addressId}")
    public AddressDto edit(@PathVariable Long addressId, @Valid @RequestBody ModifyingAddressDto addressDto) {
        return addressService.edit(addressId, addressDto);
    }

    @GetMapping("/{addressId}")
    public AddressDto detail(@PathVariable Long addressId) {
        return addressService.detail(addressId);
    }

    @PostMapping("/default/{addressId}")
    public ResponseEntity<?> setDefault(@PathVariable Long addressId) {
        addressService.setDefault(addressId);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{addressId}")
    public ResponseEntity<?> remove(@PathVariable Long addressId) {
        addressService.remove(addressId);
        return ResponseEntity.ok().build();
    }
}
